package com.moloon.game.sanguo.editor.propseditor.action;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;

import com.moloon.game.sanguo.editor.propseditor.Context;
import com.moloon.game.sanguo.editor.propseditor.ICommandIds;
import com.moloon.game.sanguo.editor.propseditor.dialog.SpecNameDialog;

public class NewItemAction extends Action {

	private final IWorkbenchWindow window;
	private int instanceNum = 0;
	private final String viewId;

	public NewItemAction(IWorkbenchWindow window, String label, String viewId) {
		this.window = window;
		this.viewId = viewId;
		setText(label);
		// The id is used to refer to the action in a menu or toolbar
		setId(ICommandIds.CMD_NEW_PROP_ITEM);
		// Associate the action with a pre-defined command, to allow key
		// bindings.
		setActionDefinitionId(ICommandIds.CMD_NEW_PROP_ITEM);
		setImageDescriptor(propseditor.Activator
				.getImageDescriptor("/icons/sample2.gif"));
	}

	public void run() {
		// if(window != null) {
		// try {
		// window.getActivePage().showView(viewId,
		// Integer.toString(instanceNum++), IWorkbenchPage.VIEW_ACTIVATE);
		// } catch (PartInitException e) {
		// MessageDialog.openError(window.getShell(), "Error",
		// "Error opening view:" + e.getMessage());
		// }
		// }
		ISelection selection = Context.getInstance().getNavigationTreeViewer()
				.getSelection();
		if (selection == null) {
			return;
		}

		if (selection.toString().equals("[主线任务]")) {
			try {
				window.getActivePage().showView(viewId,
						Integer.toString(instanceNum++),
						IWorkbenchPage.VIEW_ACTIVATE);
			} catch (Exception e) {
				MessageDialog.openError(window.getShell(), "Error",
						"Error opening view:" + e.getMessage());
			}
		}
	}
}
